﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using System.ComponentModel;

namespace ProgNetComponentsDx.Components
{
    public class MemoBgAppender : BaseBgAppender
    {
        [DefaultValue(0)]
        [Category(ProgNetComponents.Constants.Categories.ProgNetSettings)]
        public int MaxLines
        {
            get;
            set;
        }
        public MemoEdit Memo
        {
            get;
            set;
        }
        protected override void AppendFullLines(string fullLines)
        {
            if (Memo != null)
            {
                if (Memo.InvokeRequired)
                {
                    Memo.Invoke(new Action<string>(AppendFullLines), fullLines);
                    return;
                }
                Memo.AppendText(fullLines);
                if (MaxLines > 0)
                {
                    if (Memo.Lines != null && Memo.Lines.Length > MaxLines)
                    {
                        List<string> list = new List<string>();
                        list.AddRange(Memo.Lines.Skip(Memo.Lines.Length - MaxLines));
                        Memo.Lines = list.ToArray();
                    }
                }
            }
        }
    }
}
